package com.example.lets;

import java.util.List;

import com.bmob.BmobConfiguration;
import com.bmob.BmobPro;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.RequestSMSCodeListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.VerifySMSCodeListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		final CountDownTimer timer;
		final Button sendcode = (Button) this.findViewById(R.id.register_sendcode);
		
		// button back
		Button back = (Button) this.findViewById(R.id.register_back);
		back.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(RegisterActivity.this, MainActivity.class);
//				startActivity(intent);
				RegisterActivity.this.finish();
			}
		});
		
		// button register
		Button register = (Button) this.findViewById(R.id.register_register);
		register.setOnClickListener(new Button.OnClickListener() {
			
			EditText phone = (EditText) findViewById(R.id.register_phone);  
			EditText pswd1 = (EditText) findViewById(R.id.register_password);
			EditText pswd2 = (EditText) findViewById(R.id.register_confirm);
			EditText code  = (EditText) findViewById(R.id.register_code);
			
			public void onClick(View v) {
				try {
					registerClick(phone.getText().toString(), pswd1.getText().toString(),
							pswd2.getText().toString(), code.getText().toString());
				} catch (NullItemException e) {
					Toast.makeText(RegisterActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
				} catch (PasswordNotEqualException e) {
					Toast.makeText(RegisterActivity.this, "密码不相同！", Toast.LENGTH_SHORT).show();
				} catch (VerifyCodeFailException e) {
					Toast.makeText(RegisterActivity.this, "验证码不正确！", Toast.LENGTH_SHORT).show();
				}
			}
		});

		// code countdown
		timer = new CountDownTimer(45000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
				sendcode.setBackground(getResources().getDrawable(R.drawable.nonclick_code));
				sendcode.setText((millisUntilFinished / 1000) + "秒后可重发");
				sendcode.setEnabled(false);
			}

			@Override
			public void onFinish() {
				sendcode.setEnabled(true);
				sendcode.setText(null);
				sendcode.setBackground(getResources().getDrawable(R.drawable.click_code));
			}
		};

		// button send code
		sendcode.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				EditText phone = (EditText) findViewById(R.id.register_phone);
				try {
					if (phone.getText().toString().length() != 11) throw new WrongPhoneNumber();
					sendCodeClick(phone.getText().toString());
					timer.start();
				} catch (NullItemException e) {
					Toast.makeText(RegisterActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
				} catch (ReplicatePhoneException e) {
					Toast.makeText(RegisterActivity.this, "该号码已被注册！", Toast.LENGTH_SHORT).show();
				} catch (WrongPhoneNumber e) {
					Toast.makeText(RegisterActivity.this, "请输入合法的11位手机号码！", Toast.LENGTH_SHORT).show();
				}
			}
		});

	}
	
	
	// 增加一个新用户，其手机验证状态为false
	private void addUser(final String phoneNumber) throws ReplicatePhoneException {
		
		BmobQuery<User> query = new BmobQuery<User>();
		query.addWhereEqualTo("mobilePhoneNumber", phoneNumber);
		query.setLimit(1);
		
		query.findObjects(this, new FindListener<User>() {
			@Override
			public void onSuccess(List<User> object) {
				// TODO Auto-generated method stub
				if (object.size() >= 1) {
					Log.i("bmob", "查询到已存在的手机项");
					Toast.makeText(RegisterActivity.this, "该手机已被注册！", Toast.LENGTH_SHORT).show();
					return;
				}

				BmobSMS.requestSMSCode(RegisterActivity.this, phoneNumber, "验证码", new RequestSMSCodeListener() {
					@Override
					public void done(Integer smsId, BmobException ex) {
						// TODO Auto-generated method stub
						if (ex == null) { //验证码发送成功
							Log.i("bmob", "短信id：" + smsId); //用于查询本次短信发送详情
						} else {
							Log.i("bmob", "errorCode = " + ex.getErrorCode()
									+ ",errorMsg = " + ex.getLocalizedMessage());
						}
					}
				});
			}

			@Override
			public void onError(int code, String msg) {
				// TODO Auto-generated method stub
				Log.i("bmob", "查询失败");
				Toast.makeText(RegisterActivity.this, "请求发送失败，请检查网络设置！", Toast.LENGTH_SHORT).show();
			}
		});
	}
	
	// 点击发送验证码按钮的事件
	private void sendCodeClick(String phoneNumber) throws NullItemException, ReplicatePhoneException {
		if (phoneNumber == null || phoneNumber.equals(""))
			throw new NullItemException("手机号不能为空！");
		addUser(phoneNumber);
	}

	// 点击注册按钮的事件
	private void registerClick(final String phoneNumber, final String pswd1, String pswd2, String code) 
		                                throws NullItemException, PasswordNotEqualException, VerifyCodeFailException {
		if (phoneNumber == null || phoneNumber.equals(""))
			throw new NullItemException("手机号不能为空！");
		if (pswd1 == null || pswd1.equals(""))
			throw new NullItemException("密码不能为空！");
		if (pswd2 == null || pswd2.equals(""))
			throw new NullItemException("密码不能为空！");
		if (!pswd1.equals(pswd2))
			throw new PasswordNotEqualException();
		
		// 判断是否验证成功
		BmobSMS.verifySmsCode(this, phoneNumber, code, new VerifySMSCodeListener() {

			@Override
			public void done(BmobException ex) {
				// TODO Auto-generated method stub
				if (ex == null) { //短信验证码已验证成功，开始注册
					Log.i("bmob", "验证通过");
					Toast.makeText(RegisterActivity.this, "验证通过", Toast.LENGTH_SHORT).show();

					// 未查询到重复项，插入一条新记录
					final User user = new User();
					user.setUsername(phoneNumber);
					user.setMobilePhoneNumber(phoneNumber);
					user.setPassword(pswd1);
					user.setMobilePhoneNumberVerified(true);
					user.signUp(RegisterActivity.this, new SaveListener() {
						@Override
						public void onSuccess() {
							// TODO Auto-generated method stub
							Log.i("bmob", "注册成功");

							// 初始化该用户的缓存路径
							BmobConfiguration config = new BmobConfiguration.Builder(RegisterActivity.this).customExternalCacheDir(user.getUsername()).build();
							BmobPro.getInstance(RegisterActivity.this).initConfig(config);

							// 注册成功，跳转到信息完善界面
							Intent intent = new Intent();
							intent.setClass(RegisterActivity.this, CompleteInfoActivity.class);
							startActivity(intent);
							RegisterActivity.this.finish();
						}

						@Override
						public void onFailure(int code, String msg) {
							// TODO Auto-generated method stub
							Log.i("bmob", "注册失败");
							Toast.makeText(RegisterActivity.this, "注册失败，请检查网络设置！", Toast.LENGTH_SHORT).show();
						}
					});

				} else { //验证失败
					Log.i("bmob", "验证失败：code =" + ex.getErrorCode() + ",msg = " + ex.getLocalizedMessage());
					Toast.makeText(RegisterActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.register, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		finish();
		return;
	}
}
